package com.woniu.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.woniu.domain.CartItem;
import com.woniu.domain.Goods;
import com.woniu.domain.ShopCart;
import com.woniu.service.CategoryService;
import com.woniu.service.impl.CategoryServiceImpl;

/**
 * Servlet implementation class ShopCartServlet
 */
@WebServlet("/ShopCartServlet")
public class ShopCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShopCartServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer gId = Integer.parseInt(request.getParameter("gid"));				
		CategoryService cs = new CategoryServiceImpl();
		
		try {
			Goods goods = cs.findGoodsBygId(gId);
			//取出购物车
			HttpSession session = request.getSession();	        	        
	        ShopCart shopCart = (ShopCart) session.getAttribute("shopCart");
			 if(shopCart == null) {
				 shopCart = new ShopCart();
				 session.setAttribute("shopCart", shopCart);
			 }
			 shopCart.addCartItemToShopCart(gId, goods);			
			 System.out.println(gId+":\t"+goods);			 
			 request.getRequestDispatcher("shopcart.jsp").forward(request, response);
			 
			
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		     
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
